var app = getApp();
// 宏定义
const useful_function = require('../../utils/usefulfunction.js');
//获取可使用机型窗口高度windowHeight
const { windowHeight } = wx.getSystemInfoSync();
const groupinfo = 1;
const userinfo = 2;
const getintogroup = 1;
const getoutofgroup = 2;
const creategroup = 3;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        infomation_timer: "",
        uid: null,
        stop_longpress: false,
        is_longpressed: false,
        info_index_chosen: [],
        info_list: [],
    },

    isRead: function (i) {
        var that = this;
        this.setData({
            ['info_list[' + i + '].is_read']: 1,
            id_tobe_send: that.data.id_tobe_send.concat(that.data.info_list[i].id)
        })
    },

    testisReadornot: function (index) {
        var SystemInfo = wx.getSystemInfoSync();
        var query = wx.createSelectorQuery();
        var that = this;
        query.select('#we' + index).boundingClientRect().exec((rect) => {
            if (rect[0].top < SystemInfo.windowHeight && rect[0].bottom < SystemInfo.windowHeight + 20) {
                that.setData({
                    ['info_list[' + index + '].is_read']: 1,
                })
                app.globalData.notread_number--;
                that.sendisReadList(that.data.info_list[index].id);
            }
        });
    },

    longPress: function () {
        if (!this.data.stop_longpress) {
            this.setData({
                is_longpressed: true
            })
        }
    },

    getInfoList: function () {
        var that = this;
        wx.request({
            url: app.buildUrl("/message/information-index"),
            header: app.getRequestHeader(),
            data: {
                "user_id": app.globalData.userID
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
                for (var i = 0; i < resp.data.inf_data_list.length; i++) {
                    resp.data.inf_data_list[i].created_time = useful_function.getDate(resp.data.inf_data_list[i].created_time);
                    that.setData({
                        uid: app.globalData.userID,
                        info_list: [resp.data.inf_data_list[i]].concat(that.data.info_list)
                    });
                }
            }
        })
    },

    sendisReadList: function (id) {
        wx.request({
            url: app.buildUrl("/message/information-read"),
            header: app.getRequestHeader(),
            data: {
                "id_list": id
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
            }
        })
    },

    stopLongpress: function () {
        this.setData({
            stop_longpress: true
        })
    },

    beginLongpress: function () {
        this.setData({
            stop_longpress: false
        })
    },

    /**
     * @brief 根据输入处理是否同意信息
     * @param {*} index 消息的下标
     * @param {*} agree_state 同意或拒绝，同意为1，拒绝为0
     */
    setAgreeorDisagree: function (index, agree_state) {
        if (agree_state) {
            this.setData({
                ["info_list[" + index + "].done_type"]: 1,
            })
        }
        else {
            this.setData({
                ["info_list[" + index + "].done_type"]: 2,
            })
        }
        console.log("index:  ", index, "  agree_s:  ", agree_state);
        console.log("info:  ", this.data.info_list[index]);
        var that = this;
        wx.request({
            url: app.buildUrl("/group/join-agree"),
            header: app.getRequestHeader(),
            data: {
                "group_id": that.data.info_list[index].group_info.gid,
                "user_id": that.data.info_list[index].user_info.uid,
                "is_agree": agree_state,
                "inf_id": that.data.info_list[index].id
            },
            success: function (res) {
                var resp = res.data;
                console.log(resp);
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
            }
        })
    },

    pressAgree: function (event) {
        this.setAgreeorDisagree(parseInt(event.target.id.substring(5, event.target.id.length)), 1)
    },

    pressDisagree: function (event) {
        this.setAgreeorDisagree(parseInt(event.target.id.substring(8, event.target.id.length)), 0)
    },

    pressCheck: function (event) {
        var that = this;
        var info_index_tobe_set = parseInt(event.target.id.substring(8, event.target.id.length));
        var same_index_in_chosen = that.data.info_index_chosen.findIndex(function (value) {
            return value === info_index_tobe_set
        }); //寻找相同的值，返回下标
        var data_tobe_set = 'info_list[' + info_index_tobe_set + '].is_chosen';
        //正选
        if (same_index_in_chosen == -1) {
            that.setData({
                info_index_chosen: that.data.info_index_chosen.concat(info_index_tobe_set),
                [data_tobe_set]: true
            });
        }
        //反选
        else {
            var array_index = that.data.info_index_chosen;
            array_index.splice(same_index_in_chosen, 1);
            that.setData({
                info_index_chosen: array_index,
                [data_tobe_set]: false
            })
        }
    },

    goBack: function () {
        this.setData({
            info_index_chosen: [],
            is_longpressed: false
        });
        for (var i = 0; i < this.data.info_list.length; i++) {
            this.setData({
                ['info_list[' + i + '].is_chosen']: false
            })
        }
    },

    /**
     * @brief 根据下表判断某条信息同意/拒绝框是否未被点击
     * @param {*} index 待判断的下标
     * @return 未处理返回true，处理返回false
     */
    agreeorDisagreeNotSolve: function (index) {
        var info_tobe_solve = this.data.info_list[index];
        //管理员2未处理
        if (info_tobe_solve.manager_info.uid == this.data.uid && info_tobe_solve.inform_type == 2 && info_tobe_solve.done_type == 0) {
            return true
        }
        //用户3未处理
        if (info_tobe_solve.manager_info.uid != this.data.uid && info_tobe_solve.inform_type == 3 && info_tobe_solve.done_type == 0) {
            return true
        }
        return false;
    },

    deleteMessage: function () {
        var info_index_arr = this.data.info_index_chosen;
        var info_arr = this.data.info_list;
        var id_in_chosen = [];

        info_index_arr.sort((a, b) => {
            return b - a
        })

        //从后往前删就不会出问题了
        for (var i = 0; i < info_index_arr.length; i++) {
            id_in_chosen = id_in_chosen.concat(this.data.info_list[info_index_arr[i]].id);
            if (this.agreeorDisagreeNotSolve(info_index_arr[i])) {
                this.setAgreeorDisagree(info_index_arr[i], 0);
            }
            info_arr.splice(info_index_arr[i], 1);
        }
        this.setData({
            info_list: info_arr
        })

        wx.request({
            url: app.buildUrl("/message/information-remove"),
            header: app.getRequestHeader(),
            data: {
                'id_list': id_in_chosen
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({
                        "content": resp.msg
                    });
                    return;
                }
            }
        })
        this.goBack();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this;
        this.getInfoList();
        var timer = setInterval(function () {
            for (var i = 0; i < that.data.info_list.length; i++) {
                if (!that.data.info_list[i].is_read) {
                    that.testisReadornot(i);
                }
            }
        }, 1000);
        this.setData({
            info_index_chosen: [],
            is_longpressed: false,
            infomation_timer: timer
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        var that=this;
        clearInterval(that.data.infomation_timer)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        var that=this;
        clearInterval(that.data.infomation_timer)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})